package com.wholesale;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.wholesale.beans.UserAddress;
import com.wholesale.datas.WebServiceClient;
import com.wholesale.utils.Constants;
import com.wholesale.utils.MyAddressListAdapter;

public class MyAddressLayout extends LinearLayout implements OnClickListener {
	public final static int MYACCOUNT = 0;
	public final static int CHECKOUT = 1;
	private ViewFlipper viewFlipper;
	private Context context;
	private View baseView;
	private ListView bodyList;
	private Button leftButton;
	private Button rightButton;
	private TextView headView;
	private MyAddressListAdapter listAdapter;
	private RelativeLayout loadingLayout;
	private ArrayList<UserAddress> addressList;
	private int actionType;
	private UserAddress curUserAddress ;

	// 构造函数
	public MyAddressLayout(Context context, final ViewFlipper viewFlipper,
			int type,int selectIndex,UserAddress curUserAddress ) {
		super(context);
		this.viewFlipper = viewFlipper;
		this.context = context;
		this.actionType = type;
		this.curUserAddress = curUserAddress ;
		// 加载需要的属性，加载方法一的子Layout
		baseView = ((Activity) getContext()).getLayoutInflater().inflate(
				R.layout.myaddress, this);
		bodyList = (ListView) baseView.findViewById(R.id.bodyList);
		leftButton = (Button) baseView.findViewById(R.id.leftBtn);
		leftButton.setText("Back");
		leftButton.setOnClickListener(this);

		rightButton = (Button) baseView.findViewById(R.id.rightBtn);
		rightButton.setText("+");
		rightButton.setOnClickListener(this);
		// rightButton.setVisibility(View.INVISIBLE);
		headView = (TextView) baseView.findViewById(R.id.title);
		headView.setText("My Addresses");
		loadingLayout = (RelativeLayout) baseView
				.findViewById(R.id.loadingLayout);

		bodyList.setSelector(R.drawable.comments_list_bg);
		// item 单击事件
		bodyList.setOnItemClickListener(moreClieckListener);

		listAdapter = new MyAddressListAdapter(context, bodyList);
		if(actionType == CHECKOUT){
			listAdapter.setShowSelect(true);
			listAdapter.setSelected(selectIndex);
		}
		// listAdapter.setAsyncLoadData(true);
		bodyList.setAdapter(listAdapter);

		AddressAsync addressAsc = new AddressAsync(context);
		addressAsc.execute((Void) null);

	}

	// ListView 点击事件
	OnItemClickListener moreClieckListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			if (actionType == MYACCOUNT) {
				UserAddress userAddress = addressList.get(arg2);
				AddressLayout addressLayout = new AddressLayout(context,
						viewFlipper, 0,userAddress);
				viewFlipper.addView(addressLayout);
				viewFlipper.showNext();
			} else if (actionType == CHECKOUT) {
				int count = viewFlipper.getChildCount();
				View preView = viewFlipper.getChildAt(count - 2);
				if (preView.getClass().getName().equals(
						CheckOutLayout.class.getName())) {
					((CheckOutLayout) preView).setUserAddress(arg2,listAdapter
							.getItem(arg2));
				}
				viewFlipper.showPrevious();
				viewFlipper.removeView(MyAddressLayout.this);
			}

		}
	};

	public int getActionType(){
		return actionType ;
	}
	
	public void reLoad(){
		AddressAsync addressAsc = new AddressAsync(context);
		addressAsc.execute((Void) null);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.leftBtn:
			viewFlipper.showPrevious();
			viewFlipper.removeView(this);
			break;
		case R.id.rightBtn:
			AddressLayout addressLayout = new AddressLayout(context,
					viewFlipper, 0,null);
			viewFlipper.addView(addressLayout);
			viewFlipper.showNext();
			break;

		default:
			break;
		}
	}

	class AddressAsync extends AsyncTask<Void, Integer, ArrayList<UserAddress>> {
		ProgressDialog pdialog;
		long goodsCount = 1;
		boolean desc;

		public AddressAsync(Context context) {
			// pdialog = new ProgressDialog(context);
			// pdialog.setTitle("Loading...");
			// pdialog.setMessage("Please wait...");
			// pdialog.setCancelable(true);
			// pdialog.setButton("cancel", new DialogInterface.OnClickListener()
			// {
			// public void onClick(DialogInterface dialog, int i) {
			// dialog.cancel();
			// AddressAsync.this.cancel(true);
			// }
			// });
			// pdialog.show();
		}

		@Override
		protected void onPreExecute() {
			loadingLayout.setVisibility(View.VISIBLE);
		}

		@Override
		protected ArrayList<UserAddress> doInBackground(Void... params) {
			
			addressList = WebServiceClient.getUserAddress(Constants.loginResponse
					.getUserId(), Constants.loginResponse.getUserToken());
			return addressList;
		}

		protected void onPostExecute(ArrayList<UserAddress> result) {
			listAdapter.clear();
			listAdapter.addItems(result);
			if(curUserAddress != null ){
				int size = result.size();
				int curIndex = 0;
				for(int i=0;i<size;i++){
					if(result.get(i).getAddressId()== curUserAddress.getAddressId()){
						curIndex = i ;
						break ;
					}
				}
				listAdapter.setSelected(curIndex);
			}else if(result.size()>0){
				curUserAddress = result.get(0);
			}
			listAdapter.notifyDataSetChanged();
			loadingLayout.setVisibility(View.GONE);
//			pdialog.dismiss();
		}

		protected void onCancelled() {
			super.onCancelled();
		}

	}
}
